package com.hrms.controller.user;

import com.hrms.annotation.UserIgnore;
import com.hrms.entity.Room;
import com.hrms.entity.Stay;
import com.hrms.entity.User;
import com.hrms.http.ResponseResult;
import com.hrms.service.RoomService;
import com.hrms.service.StayService;
import com.hrms.service.UserService;
import com.hrms.utils.EntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Transactional
@RestController
@RequestMapping("/user/index")
public class UserIndexController {
    // 用户首页

    @Autowired
    private RoomService roomService;
    @Autowired
    private UserService userService;
    @Autowired
    private StayService stayService;

    @UserIgnore
    @RequestMapping("/init")
    public ResponseResult<List<Map<String, Object>>> init() {
        // 初始化

        // 各客房类型的余量
        int standard_double_count = roomService.typeCount("标准大床房");
        int superior_double_count = roomService.typeCount("高级大床房");
        int business_double_count = roomService.typeCount("商务大床房");
        int standard_twin_count = roomService.typeCount("标准双床房");
        int superior_twin_count = roomService.typeCount("高级双床房");
        int business_twin_count = roomService.typeCount("商务双床房");
        int standard_count = roomService.typeCount("标准间");
        int standard_family_count = roomService.typeCount("标准家庭房");
        int superior_family_count = roomService.typeCount("高级家庭房");

        // 各客房类型的价格
        String standard_double_price = roomService.selectRoomPricebyType("标准大床房");
        String superior_double_price = roomService.selectRoomPricebyType("高级大床房");
        String business_double_price = roomService.selectRoomPricebyType("商务大床房");
        String standard_twin_price = roomService.selectRoomPricebyType("标准双床房");
        String superior_twin_price = roomService.selectRoomPricebyType("高级双床房");
        String business_twin_price = roomService.selectRoomPricebyType("商务双床房");
        String standard_price = roomService.selectRoomPricebyType("标准间");
        String standard_family_price = roomService.selectRoomPricebyType("标准家庭房");
        String superior_family_price = roomService.selectRoomPricebyType("高级家庭房");

        Map<String, Object> map = new HashMap<>();
        map.put("standard_double_count", standard_double_count);
        map.put("superior_double_count", superior_double_count);
        map.put("business_double_count", business_double_count);
        map.put("standard_twin_count", standard_twin_count);
        map.put("superior_twin_count", superior_twin_count);
        map.put("business_twin_count", business_twin_count);
        map.put("standard_count", standard_count);
        map.put("standard_family_count", standard_family_count);
        map.put("superior_family_count", superior_family_count);

        map.put("standard_double_price", standard_double_price);
        map.put("superior_double_price", superior_double_price);
        map.put("business_double_price", business_double_price);
        map.put("standard_twin_price", standard_twin_price);
        map.put("superior_twin_price", superior_twin_price);
        map.put("business_twin_price", business_twin_price);
        map.put("standard_price", standard_price);
        map.put("standard_family_price", standard_family_price);
        map.put("superior_family_price", superior_family_price);

        List<Map<String, Object>> list = new ArrayList<>();
        list.add(map);
        return new ResponseResult<>("SUCCESS", list);
    }

    @RequestMapping("/check")
    public ResponseResult<Object> check(@RequestBody EntityUtil entity) {
        // 检查是否完善个人信息
        User user = new User();
        user.setUser_id(entity.getUser_id());

        if(Objects.equals(userService.selectCustomerId(user), "无")) {
            return new ResponseResult<>("ERROR_CUSTOMER", null);
        }
        return new ResponseResult<>("SUCCESS", null);
    }

    @RequestMapping("/book")
    public ResponseResult<Object> book(@RequestBody EntityUtil entity) {
        // 预订客房
        Integer count = entity.getCount();
        String[] stay_name = entity.getStay_name_all();

        // 唯一性
        Integer stay_multiple_id = 0;
        if(count == 1) {
            stay_multiple_id = 1;
        }

        // 没有完善个人信息
        User user = new User();
        user.setUser_id(entity.getUser_id());

        if(Objects.equals(userService.selectCustomerId(user), "无")) {
            return new ResponseResult<>("ERROR_CUSTOMER", null);
        }

        // 只预订一间客房
        if(stay_multiple_id == 1) {
            entity.setStay_name(stay_name[0]);
            entity.setStay_multiple_id(1);
            if(bookOne(entity).get("boolean") == "false") {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }
        }

        // 预订多间客房
        if(stay_multiple_id == 0) {
            // 第一间客房的预订
            entity.setStay_name(stay_name[0]);
            entity.setStay_multiple_id(1);

            Map<String, Object> result = bookOne(entity);
            if(result.get("boolean") == "false") {
                // 回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new ResponseResult<>("ERROR", null);
            }

            // 从第二间起的客房预订
            entity.setStay_id(Integer.parseInt(result.get("stay_id").toString()));
            for(int i = 1; i < count; i++) {
                entity.setStay_name(stay_name[i]);
                entity.setStay_multiple_id(i + 1);
                if(bookOne(entity).get("boolean") == "false") {
                    // 回滚
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return new ResponseResult<>("ERROR", null);
                }
            }
        }

        return new ResponseResult<>("SUCCESS", null);
    }

    public Map<String, Object> bookOne(EntityUtil entity) {
        // 封装预订客房操作
        Map<String, Object> map = new HashMap<>();

        Room room = new Room();
        room.setRoom_type(entity.getRoom_type());
        String room_id = roomService.getOneRoomId(room);

        // 检查客房是否空闲
        Room roomBefore = new Room();
        roomBefore.setRoom_id(room_id);
        roomBefore.setRoom_status("空闲");

        if(!roomService.checkStatue(roomBefore)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("boolean", "false");
            return map;
        }

        // 修改客房状态
        Room roomAfter = new Room();
        roomAfter.setRoom_id(room_id);
        roomAfter.setRoom_status("待支付");

        if(!roomService.updateStatue(roomAfter)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("boolean", "false");
            return map;
        }

        // 新建订单
        Stay stay = new Stay();
        stay.setStay_id(entity.getStay_id());
        stay.setStay_multiple_id(entity.getStay_multiple_id());
        stay.setRoom_id(room_id);
        stay.setRoom_status("待支付");
        stay.setUser_id(entity.getUser_id());
        stay.setEmployee_id(0);
        stay.setStay_start_time(entity.getStay_start_time());
        stay.setStay_end_time(entity.getStay_end_time());
        stay.setStay_name(entity.getStay_name());

        if(!stayService.add(stay)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("boolean", "false");
            return map;
        }

        map.put("boolean", "true");
        map.put("stay_id", stay.getStay_id());
        return map;
    }
}